<template>
	<view class="padding30rpx">
		<!-- <u-navbar :is-back="true" @custom-back="fan"></u-navbar> -->
		<view class="login-top">
			<image src="../static/logo.png"></image>
		</view>
		<view class="login-font">
			<view>您好，</view>
			<view>欢迎来到IBMG零商e学！</view>
		</view>
		<view>
			<view class="login-marg">手机号</view>
			<view class="line" style="padding-bottom: 30rpx;">
				<u-input v-model="mobile" maxlength="11" placeholder="请输入手机号" />
			</view>
		</view>
		<view>
			<view class="login-marg">密码</view>
			<view class="line" style="padding-bottom: 30rpx;">
				<u-input type="password" maxlength="18" v-model="password" placeholder="请输入密码" />
			</view>
		</view>
		<view class="dis" style="margin-top: 30rpx;">
			<view class="hui66" @tap="wang">忘记密码</view>
			<view class="hui66" @tap="rester">注册账号</view>
		</view>
		<button @tap="enter">登录</button>
		<view @tap="zan" class="hui66" style="text-align: center; margin-top: 45rpx;">暂不登录</view>
		<u-popup v-model="show" mode="bottom">
			<view class="bindwxBox u-flex">
				<view class="text u-flex-1">为了给您提供更好的服务，请绑定微信</view>
				<view class="right_btmn">
					<view @click="bindGetUserInfo">
						绑定微信
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile: '',
				password: '',
				lsToken: '',
				show: false,
				customBind: {
					background: '#00AC00',
					color: '#fff',
					border: 'none',
					height: '70rpx'
				},
				userInfo: []
			}
		},
		methods: {
			fan() {
				uni.navigateBack(1)
			},

			enter() {
				let data = {
					mobile: this.mobile,
					password: this.password,
				}
				let checkSendCode = this.$mGraceChecker.check(data, this.$mFormRule.loginByPassRule);
				if (!checkSendCode) {
					this.$mHelper.toast(this.$mGraceChecker.error)
					return;
				}
				this.$http.post('app/UserController/login', {
					phone: this.mobile,
					password: this.password
				}).then(res => {
					if (res.code == 555) {
						this.lsToken = res.data
						this.show = true
						//未授权
					} else if (res.code == 200) {
						this.$store.commit('login', {
							token: res.data
						})
						this.getvegw();
					} else {
						this.$mHelper.toast(res.message)
					}
				})
			},
			//微信授权
			bindGetUserInfo(e) {
				var _this = this;
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						console.log(res);
						//获取用户信息
						_this.userInfo = res;
						// this.updateUserInfo();
						try {
							//授权
							_this.login();
						} catch (e) {}
					},
					fail: (res) => {
						this.$mHelper.toast('微信登录授权失败')
					}
				});
			},
			login() {
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: function(res) {
						if (res.code) {
							let code = res.code;
							_this.updateUserInfo(code);
						} else {

						}
					},
				});
			},
			updateUserInfo(code) {
				uni.showLoading({
					title: '登录中...'
				});
				let _this = this;
				var params = {
					code: code,
					iv: this.userInfo.iv,
					encryptedData: this.userInfo.encryptedData,
					heads: this.userInfo.userInfo.avatarUrl,
					likenName: this.userInfo.userInfo.nickName,
					token: this.lsToken,
					region: this.userInfo.userInfo.city,
					sex: this.userInfo.userInfo.gender
				}
				this.$http.post('app/UserController/WXLogin', params).then(res => {
					if (res.code == 200) {
						//请求成功，获取用户信息
						this.$store.commit('login', {
							token: this.lsToken
						})
						this.getvegw();
						this.show = false;
					} else {
						uni.hideLoading()
						this.$mHelper.toast(res.message)
					}
				}).catch(err => {
					uni.hideLoading()
				});
			},

			//检查是否选择岗位
			getvegw() {
				this.$http.get('app/UserController/addUserPost').then(res => {
					if (res.code == 200 && res.data.yesno == false) {
						this.$http.get('app/UserController/selectUserMassges').then(rest => {
							if (rest.code == 200) {
								if (rest.data
									.characters == '企业会员'
								) {
									//企业用户
									uni.navigateTo({
										url: '/pages/Qgreet/Qgreet?name=' + rest.data
											.likename +
											'&enterprisename=' + rest.data.enterprisename
									})
								} else {
									var name = rest.data.username == null ? rest.data
										.likename : rest.data.username
									//个人用户
									uni.navigateTo({
										url: '/pages/Ggreet/Ggreet?name=' + name
									})
								}
							} else {
								this.$mHelper.toast(res.message)
							}
						})
					} else if (res.code == 200) {
						this.$http.get('app/UserController/selectUserMassges').then(res => {
							if (res.code == 200) {
								this.$store.commit('saveInfo', {
									userinfo: res.data
								})
								if (res.data.characters == '企业会员') {
									this.$mHelper.to('/pages/Qgreet/Qgreet?name=' + res.data
										.likename)
								} else {
									// this.$mHelper.to('/pages/Qgreet/Qgreet?name=' + res.data
									// 	.likename )
									uni.reLaunch({
										url: '/pages/study/study'
									})
								}
								// store.commit('saveInfo',res.data)
							} else {
								this.$mHelper.toast(res.message)
							}
						})
					} else {
						this.$mHelper.taost(res.message)
					}
				});
			},
			//忘记密码
			wang() {
				uni.navigateTo({
					url: '../wang/wang'
				})
			},
			//注册账号
			rester() {
				uni.navigateTo({
					url: '../rester/rester'
				})
			},
			//暂不登录，跳转首页
			zan() {
				uni.reLaunch({
					url: '/pages/term/term'
				})
			},
			//登录

		}
	}
</script>

<style lang="scss" scoped>
	button {
		margin-top: 256rpx;
		background-color: #00a29a;
		color: #FFFFFF;
		height: 92rpx;
		line-height: 92rpx;
		width: 100%;
	}

	input {
		font-size: 28rpx;
	}

	.login-top {
		text-align: center;
		margin: 30rpx 0;

		image {
			width: 249rpx;
			height: 124rpx;

		}
	}

	.login-font {
		font-size: 40rpx;
		font-weight: 600;
		padding-bottom: 30rpx;
	}

	.login-marg {
		margin: 30rpx 0;
		font-size: 32rpx;
	}

	.bindwxBox {
		padding: 40rpx 4%;

		.text {
			// text-align: center;
			color: #606266;
			font-size: 26rpx;
		}
	}

	.right_btmn {
		background-color: #00AC00;
		padding: 13rpx 40rpx;
		color: #FFFFFF;
		border-radius: 100rpx;
		margin-left: 50rpx;
	}
</style>
